/*
 * 文件名：NoticeInfoContreller.java
 * 版权：Copyright by www.huawei.com
 * 描述：
 * 修改人：Administrator
 * 修改时间：2016年5月23日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.zcsy.seller.web.news;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.model.RequestConstants;
import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.manage.model.base.CellInfo;
import com.zcsy.manage.model.base.ShopInfo;
import com.zcsy.manage.model.news.NoticeInfo;
import com.zcsy.manage.model.news.NoticeRead;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.service.news.INoticeInfoService;
import com.zcsy.manage.service.news.INoticeReadService;

/**
 * @Title: NoticeInfoContreller.java
 * @Package： com.zcsy.seller.web.news 
 * @Description: 小区通知控制层管理类
 * @author liuying 1075817474@qq.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2016年5月23日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Controller
@RequestMapping(value = "/noticeInfo")
public class NoticeInfoContreller {

	@Autowired
	private INoticeInfoService noticeInfoService;
	
	@Autowired
	private INoticeReadService noticeReadService;
	
	/**
	 * @Description 分页获取数据列表<br>
	 * @param pNo 起始页
	 * @param req 用户请求
	 * @return 跳转页面URL
	 * @author liuying 1075817474@qq.com
	 * @date 2016年5月23日
	 * @see 
	 */
	@RequestMapping(value="noticeInfoList" , method = RequestMethod.GET)
    public String noticeInfoList(Model model, Integer pNo, HttpServletRequest req){
		
		ShopInfo shopInfo = (ShopInfo)req.getSession().getAttribute(RequestConstants.SHOP_ID);
		
		if(pNo == null){
            pNo = Constants.PAGE_NO;
        }
		  
        //此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
        PageHelper.startPage(pNo, Constants.PAGE_SIZE);
        
        String cellId = shopInfo.getCellId();

        User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
        List<NoticeInfo> noticeInfos = noticeInfoService.selectCountList(user.getId(),cellId);
        
        PageInfo<NoticeInfo> info = new PageInfo<NoticeInfo>(noticeInfos);
        
        //未读条数查询
        int unRead = noticeInfoService.selectUnreadCount(user.getId(), cellId);
        		
		model.addAttribute("noticeInfo", info);
		model.addAttribute("currentPage", pNo);
		model.addAttribute("unRead", unRead);
		
        return "news/noticeinfo/noticeInfoList";
	}
	
	/**
	 * @Description 展示小区通知详情
	 * @param id 小区通知id
	 * @param req 用户请求
	 * @return 页面跳转URL
	 * @author liuying 1075817474@qq.com
	 * @date 2016年5月24日
	 * @see 
	 */
	@RequestMapping(value = "/noticeInfoDetail", method = RequestMethod.GET)
    public String noticeInfoDetail(Model model, String id, HttpServletRequest req){
		
		User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
		String cellId = user.getCurrentCell().getId();
		NoticeInfo noticeInfo = new NoticeInfo();
		
		//根据在通知阅读表里查询的结果判断是否插入数据
		NoticeRead noticeRead = new NoticeRead();
		noticeRead.setUserId(user.getId());
    	noticeRead.setNoticeId(id);
    	int result = noticeReadService.selectCountByUserId(noticeRead);
    	
    	if(result == 0){
			noticeRead.setId(UniqueNumberUtils.get18UniqueNumber());
			noticeRead.setUserAccount(user.getLoginname());
			noticeRead.setUserType(user.getUserType());
			noticeReadService.insert(noticeRead);
			
			try {
		        //将小区通知阅读次数加一
			    noticeInfoService.updateReadCount(id);
		        
		    } catch (Exception e) {
		        e.printStackTrace();
		    }
    	}

        //未读条数查询
        int unRead = noticeInfoService.selectUnreadCount(user.getId(), cellId);
        
    	noticeInfo = noticeInfoService.selectByPrimaryKey(id);
	    model.addAttribute("noticeInfo", noticeInfo);
		model.addAttribute("unRead", unRead);
	    
        return "news/noticeinfo/noticeInfoDetail";
    }

	/**
	 * @Description 展示小区通知的上一篇和下一篇
	 * @param flag 标记是展示上一篇和下一篇(0:上一篇；1:下一篇)
	 * @param id 当前主键id
	 * @return 页面跳转
	 * @author liuying 1075817474@qq.com
	 * @date 2016年9月23日
	 * @see 
	 */
	@RequestMapping(value = "lastOrNextOne", method = RequestMethod.GET)
	public String lastOrNextOne(Model model, Integer flag, String id, HttpServletRequest req) {
		
		User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
		String cellId = user.getCurrentCell().getId();
		NoticeInfo noticeInfo = new NoticeInfo();

    	List<String> ids = noticeInfoService.selectIdList(cellId);
    	for(int i=0; i<ids.size(); i++){
    		if(id.equals(ids.get(i))){
    			//当需要展示上一篇且有上一篇时
    			if(flag == 0 && i >0){
    				id = ids.get(i-1);
    				break;
    			}
    			//当需要展示下一篇且有下一篇时
    			if(flag == 1 && i <ids.size()-1){
    				id = ids.get(i+1);
    				break;
    			}
    		}
    	}
		
		//根据在通知阅读表里查询的结果判断是否插入数据
		NoticeRead noticeRead = new NoticeRead();
		noticeRead.setUserId(user.getId());
    	noticeRead.setNoticeId(id);
    	int result = noticeReadService.selectCountByUserId(noticeRead);
    	
    	if(result == 0){
			noticeRead.setId(UniqueNumberUtils.get18UniqueNumber());
			noticeRead.setUserAccount(user.getLoginname());
			noticeRead.setUserType(user.getUserType());
			noticeReadService.insert(noticeRead);
			
			try {
				//将小区通知阅读次数加一
			    noticeInfoService.updateReadCount(id);
		        
		    } catch (Exception e) {
		        e.printStackTrace();
		    }
    	}
    	
    	noticeInfo = noticeInfoService.selectByPrimaryKey(id);
	    model.addAttribute("noticeInfo", noticeInfo);
	    
        return "news/noticeinfo/noticeInfoDetail";
    }
}
